package gw.com.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.gwtsz.gts2.hx.R;

/**
 * @author jett
 * @since 2017-10-18.
 */
public class DeleteConfirmDialog implements View.OnClickListener {

    private final Context context;
    private Dialog dialog;

    private OnDeleteListener listener;

    public DeleteConfirmDialog(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        View root = View.inflate(context, R.layout.dialog_delete_confirm, null);
        initView(root);

        ViewGroup.LayoutParams contentParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog = new Dialog(context, R.style.dialog_no_title);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(root, contentParam);

        /*Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.BOTTOM;
            params.width = DeviceUtil.instance().getScreenPixelsWidth(context);
            window.setAttributes(params);
            window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        }*/
    }

    private void initView(View root) {
        root.findViewById(R.id.tv_delete).setOnClickListener(this);
        root.findViewById(R.id.tv_cancel).setOnClickListener(this);
    }

    public void show() {
        if (dialog == null) {
            init();
        }
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public DeleteConfirmDialog listener(OnDeleteListener listener) {
        this.listener = listener;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            switch (v.getId()) {
                case R.id.tv_delete:
                    listener.onDelete(this, v);
                    break;
                default:
                    break;
            }
        }
        dismiss();
    }

    public interface OnDeleteListener {
        void onDelete(DeleteConfirmDialog dialog, View view);
    }

}
